package com.xf.controller;


import com.xf.common.core.entity.FebsResponse;
import com.xf.common.core.entity.QueryRequest;
import com.xf.common.core.entity.constant.StringConstant;
import com.xf.common.core.entity.dmp.DmpAuthUser;
import com.xf.common.core.entity.system.SystemUser;
import com.xf.common.core.exception.FebsException;
import com.xf.common.core.utils.FebsUtil;
//import com.xf.service.IUserService;
import com.xf.service.DmpAuthUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MrBird
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("user")
public class UserController {

    private final DmpAuthUserService userService;
    private final PasswordEncoder passwordEncoder;

    @GetMapping("index")
    public FebsResponse index() {
        Map<String, Object> data = new HashMap<>(5);
        // 获取系统访问记录
        data.put("totalVisitCount", 1);
        data.put("todayVisitCount", 1);
        data.put("todayIp", 1);
        // 获取近期系统访问记录
        List<Map<String, Object>> lastTenVisitCount = new ArrayList<>();
        data.put("lastTenVisitCount", lastTenVisitCount);
        DmpAuthUser param = new DmpAuthUser();
        param.setUserName(FebsUtil.getCurrentUsername());
        List<Map<String, Object>> lastTenUserVisitCount = new ArrayList<>();
        data.put("lastTenUserVisitCount", lastTenUserVisitCount);
        return new FebsResponse().data(data);
    }
    @GetMapping
    @PreAuthorize("hasAuthority('user:view')")
    public FebsResponse userList(QueryRequest queryRequest, DmpAuthUser user) {
        Map<String, Object> dataTable = FebsUtil.getDataTable(userService.findUserDetailList(user, queryRequest));
        return new FebsResponse().data(dataTable);
    }

    @GetMapping("check/{username}")
    public boolean checkUserName(@NotBlank(message = "{required}") @PathVariable String username) {
        return this.userService.findByName(username) == null;
    }

    @PostMapping
    @PreAuthorize("hasAuthority('user:add')")
    public void addUser(@Valid DmpAuthUser user) {
        this.userService.createUser(user);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('user:update')")
    public void updateUser(@Valid DmpAuthUser user) {
        this.userService.updateUser(user);
    }


    @DeleteMapping("/{userIds}")
    @PreAuthorize("hasAuthority('user:delete')")
    public void deleteUsers(@NotBlank(message = "{required}") @PathVariable String userIds) {
        String[] ids = userIds.split(StringConstant.COMMA);
        this.userService.deleteUsers(ids);
    }

    @PutMapping("profile")
    public void updateProfile(@Valid DmpAuthUser user) throws FebsException {
        this.userService.updateProfile(user);
    }

    @PutMapping("avatar")
    public void updateAvatar(@NotBlank(message = "{required}") String avatar) {
        this.userService.updateAvatar(avatar);
    }

    @GetMapping("password/check")
    public boolean checkPassword(@NotBlank(message = "{required}") String password) {
        String currentUsername = FebsUtil.getCurrentUsername();
        DmpAuthUser user = userService.findByName(currentUsername);
        return user != null && passwordEncoder.matches(password, user.getPassword());
    }

    @PutMapping("password")
    public void updatePassword(@NotBlank(message = "{required}") String password) {
        userService.updatePassword(password);
    }

    @PutMapping("password/reset")
    @PreAuthorize("hasAuthority('user:reset')")
    public void resetPassword(@NotBlank(message = "{required}") String usernames) {
        String[] usernameArr = usernames.split(StringConstant.COMMA);
        this.userService.resetPassword(usernameArr);
    }

}
